package com.zyq.controller;

import com.github.pagehelper.PageInfo;
import com.zyq.exception.CustomException;
import com.zyq.mapper.RoleMapper;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.TopicClassification;
import com.zyq.service.ITopicClassificationService;
import com.zyq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帖子分类Controller
 *
 * @author zyq
 * @date 2021-12-08
 */
@Api( tags = "帖子分类接口")
@RestController
@RequestMapping("/classification")
public class TopicClassificationController
{
    @Autowired
    private ITopicClassificationService topicClassificationService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private RoleMapper roleMapper;
    /**
     * 获取帖子分类详细信息
     */
    @GetMapping(value = "/{id}")
    public ResponseBean getInfo(@PathVariable("id") Long id)
    {
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)",topicClassificationService.selectTopicClassificationById(id));
    }

    /**
     * 查询帖子分类列表
     */
    @ApiOperation("查询所有帖子分类列表")
    @GetMapping("/list")
    @RequiresAuthentication
    public ResponseBean list(@RequestHeader String Authorization)
    {

        List<TopicClassification> list = topicClassificationService.selectTopicClassificationListAll();
//        PageInfo<TopicClassification> selectPage = new PageInfo<>(list);
        if (list == null || list.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        if(roleMapper.gerRoleByUserId(userUtil.getUserId()).equals("普通用户")){
            list.remove(1);
            list.remove(1);
            list.remove(1);
        }
        if(roleMapper.gerRoleByUserId(userUtil.getUserId())=="医生"){
            list.remove(3);
        }
        if(roleMapper.gerRoleByUserId(userUtil.getUserId())=="糖尿病患者"){
            list.remove(2);
            list.remove(2);
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", list);
    }


//    /**
//     * 获取帖子分类详细信息
//     */
//    @ApiOperation("获取帖子分类详细信息")
//    @GetMapping(value = "/{id}")
//    public ResponseBean getInfo(@PathVariable("id") Long id)
//    {
//        Map<String, Object> result = new HashMap<String, Object>(16);
//        result.put("data", topicClassificationService.selectTopicClassificationById(id));
//        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
//    }

    /**
     * 新增帖子分类
     */
    @ApiOperation("新增帖子分类")
    @PostMapping
    public ResponseBean add(@RequestBody TopicClassification topicClassification)
    {
        if(topicClassificationService.insertTopicClassification(topicClassification)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 修改帖子分类
     */
    @ApiOperation("修改帖子分类")
    @PutMapping
    public ResponseBean edit(@RequestBody TopicClassification topicClassification)
    {
        if(topicClassificationService.updateTopicClassification(topicClassification)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 删除帖子分类
     */
    @ApiOperation("删除帖子分类")
    @DeleteMapping("/{ids}")
    public ResponseBean remove(@PathVariable Long[] ids)
    {
        if(topicClassificationService.deleteTopicClassificationByIds(ids)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }
}
